package com.coolgor.axnut.service.airuc.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.airuc.CollegeDao;
import com.coolgor.axnut.dto.airuc.CollegeExecution;
import com.coolgor.axnut.dto.auth.LocalUserExecution;
import com.coolgor.axnut.entity.airuc.College;
import com.coolgor.axnut.enums.airuc.CollegeStateEnum;
import com.coolgor.axnut.enums.auth.LocalUserStateEnum;
import com.coolgor.axnut.exceptions.airuc.CollegeOperationException;
import com.coolgor.axnut.exceptions.auth.LocalUserOperationException;
import com.coolgor.axnut.service.airuc.CollegeService;

@Service
public class CollegeServiceImpl implements CollegeService {
	private static Logger log = LoggerFactory.getLogger(CollegeServiceImpl.class);
	
	@Autowired
	private CollegeDao collegeDao;
	
	
	@Override
	public College getCollege(Long collegeID) {
		return collegeDao.queryCollegeById(collegeID);
	}

	@Override
	public CollegeExecution getCollegeList(College collegeCondition) {
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<College> collegeList = collegeDao.queryCollegeList(collegeCondition);
		//依据相同的查询条件，返回查询结果总数
		int count = collegeDao.queryCollegeCount(collegeCondition);
		CollegeExecution result = new CollegeExecution();
		if (collegeList != null) {
			result.setState(CollegeStateEnum.SUCCESS.getState());
			result.setStateInfo(CollegeStateEnum.SUCCESS.getStateInfo());
			result.setCollegeList(collegeList);
			result.setCount(count);
		} else {
			result.setState(CollegeStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public CollegeExecution addCollege(College college) {
		// 空值判断
		if (college == null) {
			return new CollegeExecution(CollegeStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 赋初始值
			college.setCreateTime(new Date());
			college.setLastEditTime(new Date());
			// 添加
			effectedNum = collegeDao.insertCollege(college);
			if (effectedNum <= 0) {
				throw new CollegeOperationException("学院信息创建失败");
			} 
		} catch (Exception e) {
			throw new CollegeOperationException("addCollege error:" + e.getMessage());
		}
		return new CollegeExecution(CollegeStateEnum.SUCCESS, college);
	}

	@Override
	@Transactional
	public CollegeExecution modifyCollege(College college) {
		// 空值判断
		if (college == null) {
			return new CollegeExecution(CollegeStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新用户信息
		try {
			// 赋初始值
			college.setLastEditTime(new Date());
			// 更新操作
			effectedNum = collegeDao.updateCollege(college);
			if (effectedNum <= 0) {
				throw new CollegeOperationException("学院信息更新失败");
			} 
		} catch (Exception e) {
			throw new CollegeOperationException("Modify College Information error:" + e.getMessage());
		}
		return new CollegeExecution(CollegeStateEnum.SUCCESS, college);
	}

	@Override
	@Transactional
	public CollegeExecution removeCollege(College college) {
		// 空值判断
		if (college == null) {
			return new CollegeExecution(CollegeStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = collegeDao.deleteCollege(college);
			if (effectedNum <= 0) {
				throw new CollegeOperationException("学院信息删除失败");
			} 
		} catch (Exception e) {
			throw new CollegeOperationException("Delete College Information error:" + e.getMessage());
		}
		return new CollegeExecution(CollegeStateEnum.SUCCESS, college);
	}

	@Override
	@Transactional
	public CollegeExecution batchRemoveCollege(List<College> collegeList) {
		// 空值判断
		if (collegeList == null) {
			return new CollegeExecution(CollegeStateEnum.NULL);
		}
		List<Long> collegeIDList = new ArrayList<Long>();
		for (int i=0; i<collegeList.size(); i++) {
			collegeIDList.add(collegeList.get(i).getCollegeID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = collegeDao.batchDeleteCollege(collegeIDList);
			if (effectedNum <= 0) {
				throw new CollegeOperationException("学院信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new CollegeOperationException("Batch delete College Information error:" + e.getMessage());
		}
		CollegeExecution ce = new CollegeExecution(CollegeStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		ce.setCount(effectedNum);
		return ce;
	}

}
